'use strict';

angular.module('Initialize').controller('WorkLocationBindController', ['$scope', '$state', 'PublicModule', 'PublicEnum', 'PublicFunction', '$location', 'Authentication',
    function($scope, $state, PublicModule, PublicEnum, PublicFunction, $location, Authentication) {
        $scope.btnSave = function(){
            //收集当前作业的绑定记录
            for(var i=0; i<$scope.Module.StationWork.Records.length; i++){
                if($scope.Module.StationWork.Records[i].Work === $scope.Module.Work.FocusedRecord._id){
                    $scope.Module.StationWork.Records.splice(i, 1);
                    i--;
                }
            }
            $scope.Module.StationWork.Records = $scope.Module.StationWork.Records.concat($scope.Module._StationWork.Records);
            for(var i=0; i<$scope.Module.StationWork.Records.length; i++){
                if($scope.Module.StationWork.Records[i].WorkLocation._id == undefined){
                    PublicFunction.alert('数据错误', '工位不能为空');
                    return;
                }
            }
            $scope.Module.StationWork.save(function(){
                $scope.Module.WorkLocation.clearRecords();
                $scope.Module.StationWork.clearRecords();
                $scope.Module._StationWork.clearRecords();
                $scope.Module.Work.load();
            });
        };
        $scope.btnCancel = function(){
            $state.reload();
        };
        var oldWorkFocusedRecord = null;
        $scope.rowChange = function(){
            //收集当前作业的绑定记录
            if(oldWorkFocusedRecord != null){
                for(var i=0; i<$scope.Module.StationWork.Records.length; i++){
                    if($scope.Module.StationWork.Records[i].Work === oldWorkFocusedRecord._id){
                        $scope.Module.StationWork.Records.splice(i, 1);
                        i--;
                    }
                }
                $scope.Module.StationWork.Records = $scope.Module.StationWork.Records.concat($scope.Module._StationWork.Records);
            }

            oldWorkFocusedRecord = $scope.Module.Work.FocusedRecord;

            //载入当前作业的绑定记录
            if($scope.Module.Work.FocusedRecord.IsLoadStationWork){
                var _StationWorkRecords = [];
                for(var i=0; i<$scope.Module.StationWork.Records.length; i++){
                    if($scope.Module.StationWork.Records[i].Work === $scope.Module.Work.FocusedRecord._id){
                        _StationWorkRecords.push($scope.Module.StationWork.Records[i]);
                    }
                }
                $scope.Module._StationWork.Records = _StationWorkRecords;
                $scope.Module._StationWork.RecordCount = _StationWorkRecords.length;
                $scope.Module._StationWork.firstPage();
            }
            else{
                $scope.Module.StationWork.find({Work: $scope.Module.Work.FocusedRecord._id}, null, function(records){
                    $scope.Module.Work.FocusedRecord.IsLoadStationWork = true;
                    $scope.Module._StationWork.clearRecords();

                    if(records.length > 0){
                        var workLocationIds = [];
                        for(var i=0; i<records.length; i++){
                            workLocationIds.push(records[i].WorkLocation);
                        }
                        $scope.Module.WorkLocation.find({_id: {$in: workLocationIds}}, {Name: 1}, function(wlReords){
                            for(var i=0; i<records.length; i++){
                                for(var j=0; j<wlReords.length; j++){
                                    if(records[i].WorkLocation === wlReords[j]._id){
                                        records[i].WorkLocation = {
                                            _id: wlReords[j]._id,
                                            Code: wlReords[j].Code,
                                            Name: wlReords[j].Name
                                        };

                                        records[i].DepartmentName = wlReords[j].Department.Name;
                                        break;
                                    }
                                }
                            }

                            $scope.Module._StationWork.Records = records;
                            $scope.Module._StationWork.RecordCount = records.length;
                            $scope.Module._StationWork.firstPage();
                            $scope.Module.StationWork.Records = $scope.Module.StationWork.Records.concat(records);
                        });                    
                    }
                    else{
                        $scope.Module._StationWork.refreshDataGrid();
                    }
                });
            }
        };

        $scope.addRow = function(){
            $scope.Module._StationWork.FocusedRecord.Work = $scope.Module.Work.FocusedRecord._id;
        };
        $scope.deleteRow = function(record){
            for(var i=0; i<$scope.Module.StationWork.Records.length; i++){
                if(record._id == $scope.Module.StationWork.Records[i]._id){
                    $scope.Module.StationWork.deleteRecord($scope.Module.StationWork.Records[i]);
                    break;
                }
            }
        };
        $scope.cellValueChange = function(columnName, newValue, oldValue){
            if(columnName === "WorkLocation.Code"){
                if(newValue.length > 0){
                    $scope.Module._StationWork.FocusedRecord.DepartmentName = newValue[0].Department.Name;
                }
                else{
                    $scope.Module._StationWork.FocusedRecord.DepartmentName = '';
                }
                $scope.Module._StationWork.refreshDataGridRows();
            }
        };

        $scope.refBeforeShow = function(refScope) {
            var workLocationIds = [];
            for (var i = 0; i < $scope.Module._StationWork.Records.length; i++) {
                if ($scope.Module._StationWork.Records[i].WorkLocation !== undefined && $scope.Module._StationWork.Records[i].WorkLocation !== "") {
                    workLocationIds.push($scope.Module._StationWork.Records[i].WorkLocation._id);
                }
            }
            refScope.Module.QueryParameter.Condition = {
                _id: {
                    $nin: workLocationIds
                }
            };
        };

        PublicModule.initModule($scope, 'Work');
        $scope.Module.Work.LoadMode = PublicEnum.LoadMode.CacheAndPaging;
        $scope.Module.Work.PageSize = 15;
        $scope.Module.Work.load();

    	PublicModule.initModule($scope, 'StationWork');
        $scope.Module.StationWork.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.StationWork.PageSize = 15;
        // $scope.Module.StationWork.QueryParameter.Populate = {path: 'WorkLocation', select: 'Code Name'};

        PublicModule.initModule($scope, 'WorkLocation');
        $scope.Module.WorkLocation.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.WorkLocation.PageSize = 15;
        $scope.Module.WorkLocation.QueryParameter.Populate = {path: 'Department', select: 'Name'};

        PublicModule.initModule($scope, '_StationWork');
        $scope.Module._StationWork.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module._StationWork.PageSize = 15;
    }
]);